package org.balabala.user.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.balabala.user.entity.CommentEntity;
import org.balabala.user.service.CommentService;
import org.balabala.user.utils.PageUtils;
import org.balabala.user.utils.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 
 *评论信息
 * @author hanshanxi
 * @email 3176988793@qq.com
 * @date 2021-09-04 09:31:53
 */
@RestController
@RequestMapping("balabala/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    
    public Resp list(@RequestParam Map<String, Object> params){
        PageUtils page = commentService.queryPage(params);

        return Resp.ok().put("page", page);
    }

    //后台管理批量查询信息
    @RequestMapping("/batchlist")
    public Resp list(@RequestParam Integer page,@RequestParam Integer limit,@RequestParam String key){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("key",key);
        hashMap.put("page",page.toString());
        hashMap.put("limit",limit.toString());
        PageUtils pageUtils = commentService.queryPageBatchList(hashMap);
        return Resp.ok().put("data",pageUtils);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{commentId}")

    public Resp info(@PathVariable("commentId") Integer commentId){
		CommentEntity comment = commentService.getById(commentId);

        return Resp.ok().put("comment", comment);
    }

    @RequestMapping("/info")
    public Resp info(@RequestParam Long uid, @RequestParam Integer page, @RequestParam Integer limit){
        Map<String,Object> hashMap = new HashMap<>();
        hashMap.put("uid",uid);
        hashMap.put("page",page.toString());
        hashMap.put("limit",limit.toString());
        PageUtils comment = commentService.listByUid(hashMap);
        if(comment == null){
            return Resp.error(500,"查询失败");
        }else {
            return Resp.ok().put("data", comment);
        }

    }

    /**
     * 保存,添加单条评论
     */
    @RequestMapping("/save")
    public Resp save(@RequestBody CommentEntity comment){
        comment.setMarkTime(new Date());
		return commentService.saveComment(comment);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public Resp update(@RequestBody CommentEntity comment){
		commentService.update(comment,new QueryWrapper<CommentEntity>().eq("uid",comment.getUid()).eq("video_id",comment.getVideoId()));

        return Resp.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/batchdelete")
    public Resp delete(@RequestBody Long[] commentIds){
        try {
            boolean bool = commentService.batchDelete(commentIds);
            return Resp.ok();
        } catch (Exception e) {
            return Resp.error(500,"删除失败");
        }

    }

    @RequestMapping("/delete")
    public Resp delete(@RequestBody Long commentId){
        return commentService.removeByCommentId(commentId);


    }



}
